package cn.liutao.order.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.liutao.common.exception.OrderException;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.order.entity.vo.OrderVo;
import cn.liutao.order.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liutao
 * @Description 获取订单详情接口
 * @date 2023/11/2 18:56
 * @Version 1.0
 */
@WebServlet("/order/item")
public class OrderItemServlet extends HttpServlet {

    private OrderService orderService;
    @Override
    public void init() throws ServletException {
        orderService= OrderService.getInstance();
    }

    /**
     * 获取订单详情接口
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idStr = req.getParameter("id");
        Integer id=null;
        if (StrUtil.isNotBlank(idStr))id= Integer.valueOf(idStr);
        String orderNo = req.getParameter("orderNo");
        if (ObjectUtil.isNull(id)&&StrUtil.isBlank(orderNo)){
            throw new OrderException("请选择订单");
        }
        OrderVo orderVo = null;
        if (StrUtil.isNotBlank(orderNo)){
            orderVo = orderService.selectVoByNo(orderNo);
        }else{
            orderVo = orderService.selectById(id);
        }
        //通过订单号查询支付单号
        String payNo = orderService.selectPayNoByOrderNo(orderNo);
        orderVo.setPayNo(payNo);
        ServletUtils.ok(resp,orderVo);
    }


}
